﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace proj01
{
    public class Terrain
    {
        #region fields
        private Game1 game;

        private Model ground;
        private Model sky;

        private Vector3 position1;
        private Vector3 position2;
        #endregion

        #region properties
        #endregion

        #region construction and initilization
        public void LoadContent(ContentManager content)
        {
            ground = content.Load<Model>("GrassField");
            sky = content.Load<Model>("Sky");
        }

        public void Initilize()
        {
            position1 = new Vector3(0, 0, 0);
            position2 = new Vector3(0, -100, 0);
        }

        public Terrain(Game1 game)
        {
            this.game = game;
        }
        #endregion

        #region draw

        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            DrawModel(graphics, ground, Matrix.CreateScale(100) * Matrix.CreateTranslation(position1));
            DrawModel(graphics, sky, Matrix.CreateScale(2) * Matrix.CreateTranslation(position2));
        }


        private void DrawModel(GraphicsDeviceManager graphics, Model model, Matrix world)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);


            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * world;
                    effect.View = game.Camera.View;
                    effect.Projection = game.Camera.Projection;
                }
                mesh.Draw();
            }
        }
        #endregion
    }
}
